﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMSAssistant.Model;
using System.Collections.ObjectModel;
using System.Linq;

namespace SMSAssistant.ViewModels
{
    public class BackupViewModel : NotificationObject
    {
        MessageDataContext msgDB;

        public MessageDataContext MsgDB
        {
            get { return msgDB; }
            set { msgDB = value; }
        }
        ObservableCollection<Backup> backups;

        bool loading;

        public bool Loading
        {
            get { return loading; }
            set
            {
                loading = value;
                NotifyPropertyChanged("Loading");
            }
        }

        public ObservableCollection<Backup> Backups
        {
            get { return backups; }
            set
            {
                backups = value;
                NotifyPropertyChanged("Backups");
                NotifyPropertyChanged("HasBack");
            }
        }

        bool hasBack;

        public bool HasBack
        {
            get { return hasBack; }
            set
            {
                hasBack = value;
                NotifyPropertyChanged("HasBack");
            }
        }

        public BackupViewModel()
        {
            msgDB = new MessageDataContext(MessageDataContext.DBConnectionString);

            LoadBacks();
        }

        private void LoadBacks()
        {
            if (msgDB.Backups.Count() > 0)
                Backups = new ObservableCollection<Backup>(msgDB.Backups.OrderByDescending((b) => b.BackupTime));
            else
                Backups = new ObservableCollection<Backup>();
        }

        public void Insert(Backup backup)
        {
            msgDB.Backups.InsertOnSubmit(backup);

            msgDB.SubmitChanges();

            Backups.Insert(0,backup);
        }

        internal void Delete(Backup backup)
        {
            msgDB.Backups.DeleteOnSubmit(backup);

            msgDB.SubmitChanges();

            Backups.Remove(backup);
        }

        internal void Restore(System.Collections.Generic.List<Message> messages)
        {
            msgDB.Messages.DeleteAllOnSubmit(msgDB.Messages);

            msgDB.Messages.InsertAllOnSubmit(messages);

            msgDB.SubmitChanges();
            //throw new NotImplementedException();
        }

        internal void MergeBackup(System.Collections.Generic.List<Backup> backups)
        {
            foreach (var back in msgDB.Backups)
            {
                var bkp = backups.FirstOrDefault(b => b.FileID == back.FileID);
                if (bkp != null)
                {
                    backups.Remove(bkp);
                }
                else
                {
                    msgDB.Backups.DeleteOnSubmit(back);
                }
            }

            if (backups.Count > 0)
            {
                msgDB.Backups.InsertAllOnSubmit(backups);
            }

            msgDB.SubmitChanges();
            //throw new NotImplementedException();
            LoadBacks();
        }
    }
}
